package com.example.demoserver.controller;

import com.example.demoserver.entity.InteAppalarm;
import com.example.demoserver.entity.InteApplog;
import com.example.demoserver.entity.InteLog;
import com.example.demoserver.service.InteAppalarmService;
import com.example.demoserver.service.InteApplogService;
import com.example.demoserver.service.InteLogService;
import com.example.demoserver.service.TaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/alarm")
@RequiredArgsConstructor
@Component
public class InteAppalarmController {
    private final InteAppalarmService inteAppalarmService;

    @Autowired
    private TaskService taskService;
    @GetMapping("/selectall")
    public List<InteAppalarm> selectAll () {
        List<InteAppalarm> list=inteAppalarmService.selectAll();
        return list;
    }

    @GetMapping("/istart")
    public boolean isStart () {

        return true;
    }
    Integer Ltype=0;
    String[] types = {"无异常", "运行状态异常", "通信异常", "安全漏洞异常", "版本过低异常", "微服务状态异常"};
    String[] str1={"设备在线服务未启动","网络异常"};
    String[] str2={"接收数据为空","数据格式错误","数据不属于该设备"};
    String[] str3={"出现安全漏洞"};
    String[] str4={"版本过低异常"};
    String[] str5={"设备管理APP状态异常","生产管理APP状态异常","运营管理APP状态异常"};

    @GetMapping("/setexception")
    public void setException(Integer type) {
        if(type==0){
            taskService.stopTask();
            InteAppalarm inteAppalarm=new InteAppalarm();
            inteAppalarm.setAlarmtime(Util.getNowTime());
            inteAppalarm.setAlarminfo(types[Ltype]+"清除");
            inteAppalarm.setAlarmtype(0);
            inteAppalarmService.save(inteAppalarm);
            Ltype=type;
        }else{
            Ltype=type;
            taskService.startTask(Ltype,inteAppalarmService);
        }

    }
    @GetMapping("/exceptios")
    public Integer getExceptios() {
        return Ltype;
    }
    public  InteAppalarm exceptions(Integer type){
        InteAppalarm inteLog=new InteAppalarm();
        inteLog.setAlarmtype(type);
        Random random = new Random();
        int delay=0;
        switch (type) {
            case 1:
                delay = (int) (Math.random() * str1.length);
                inteLog.setAlarminfo(str1[delay]);
                break;
            case 2:
                delay = (int) (Math.random() * str2.length);
                inteLog.setAlarminfo(str2[delay]);
                break;
            case 3:
                delay = (int) (Math.random() * str3.length);
                inteLog.setAlarminfo(str3[delay]);
                break;
            case 4:
                delay = (int) (Math.random() * str4.length);
                inteLog.setAlarminfo(str4[delay]);
                break;
            case 5:
                delay = (int) (Math.random() * str5.length);
                inteLog.setAlarminfo(str5[delay]);
                break;
        }
        return inteLog;
    }
}
